/**
 * Created by caosh on 2018/1/19 0019.
 */
var CartOrder = new Vue({
    el: '#cart-order',
    data: {
        tip: '确认订单',
        store: {}, // 选择的门店信息
        products: [], // 订单中的商品信息
        total: 0, // 总金额
        discount: 0, // 优惠
        discountTemp: 0, // 优惠信息临时变量
        order: {remark: '', orderPay: '微信支付'}, // 订单信息
        show: 'order', // 页面显示控制标识
        storeList: [] // 所有门店信息
    },
    created: function () {
        this.store = this.getDefaultStore();
    },
    computed: {
        // 计算面板高度 （查看清单、选择门店面板） 计算方式：屏幕高度-topbar的高度
        panelHeight: function() {
            return document.body.clientHeight - $('.top-bar').height();
        }
    },
    mixins: [SystemMixin],
    methods: {
        goBack: function () {
            if (this.show == 'order') {
                store.remove('order');
                window.history.back(-1);
            } else {
                this.show = 'order';
                this.tip = '确认订单';
            }
        },
        // 根据索引获取产品信息
        getProduct: function (index) {
            if (this.products.length>index) {
                return this.products[index];
            } else {
                return null;
            }
        },
        // 获取优惠信息
        getDiscount: function () {
            var _this = this;
            // 获取优惠信息
            /**
             $.get(url, function(result){
                _this.discount = result.discount;
                _this.discountTemp = _this.discount;
             }, 'json');
             */
            var result = {discount: 5};
            _this.discount = result.discount;
            _this.discountTemp = _this.discount;
        },
        // 获得订单总金额
        getTotal: function () {
            this.total = 0;
            this.products.forEach((p, i, arr) => { 
                this.total += (p.unitPrice*p.num);
            });
        },
        // 切换支付方式
        toChangeOrderPay: function (orderPay) {
            if (orderPay == '微信支付') {
                this.order.orderPay = '微信支付';
                this.discountTemp = this.discount;
            } else if (orderPay == '货到付款') {
                this.order.orderPay = '货到付款';
                this.discountTemp = 0;
            }
        },
        // 查看清单
        showProducts: function () {
            this.show = 'products';
            this.tip = '查看清单';
        },
        // 选择门店
        showStores: function () {
            this.show = 'store';
            this.tip = '选择门店';
            this.getStores();
        },
        // 获得所有门店
        getStores: function () {
            var _this = this;
            // 后台获取门店信息
            /**
             $.get(url, function(result){
                _this.storeList = result;
             }, 'json');
             */
            var result = [
                {name: '门店名', region: '广东省广州市海珠区', location: '景泰街道到到 100000号', phone: '13745678910', personName: '路人甲', business: '', isDefault: true},
                {name: '门店名', region: '广东省广州市海珠区', location: '景泰街道到到 100000号', phone: '13745678910', personName: '路人乙', business: '', isDefault: false},
                {name: '门店名', region: '广东省广州市海珠区', location: '景泰街道到到 100000号', phone: '13745678910', personName: '路人丙', business: '', isDefault: false},
                {name: '门店名', region: '广东省广州市海珠区', location: '景泰街道到到 100000号', phone: '13745678910', personName: '路人丁', business: '', isDefault: false},
                {name: '门店名', region: '广东省广州市海珠区', location: '景泰街道到到 100000号', phone: '13745678910', personName: '路人戊', business: '', isDefault: false}
            ];
            _this.storeList = result;
        },
        // 新增门店
        toNewStore: function () {
            store.set('storeFlag', 'new'); // 新增门店标识
            window.location.href = '../mine/store.html';
        },
        // 编辑门店
        editStore: function (storeObj) {
            store.set('storeFlag', 'edit-order'); // 编辑门店标识
            store.set('store', storeObj);
            window.location.href = '../mine/store.html';
        },
        // 继续购物
        goShopping: function () {
            window.location.href='../product/index.html'
        },
        // 立即支付
        pay: function () {
            /*this.showToast('支付成功', 'success', 1000, 'black');*/
            this.showToast('支付失败', 'error', 1000, 'black');
        }
    },
    mounted: function () {
        // 从编辑门店或新增门店返回时 显示选择门店页面
        if (store.get('storeFlag') != undefined) {
            this.showStores();
            store.remove('storeFlag');
        }
        this.getDiscount();
        this.products = store.get('order') && store.get('order').products || [];
        this.total = store.get('order') && store.get('order').total || 0;

        // 移除当前选中门店
        store.remove('currentStore');
    },
    watch: {
        discountTemp: function(val, oldVal) {
            this.getTotal();
            this.total -= val;
        }
    }
});